package ciyuan.Structure.MyFile;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 150149
 */
public class StructureBuilder {

    public static List<BlockInfo> tempsave = new ArrayList<>();

    public static List<BlockInfo> onSave(World world, BlockPos blockPos1 ,BlockPos blockPos2) {
        if (world==null) {
            return null;
        }
        List<BlockInfo> blockInfoList = new ArrayList<>();

        int x1=Math.min(blockPos1.getX(),blockPos2.getX());
        int x2=Math.max(blockPos1.getX(),blockPos2.getX());
        int y1=Math.min(blockPos1.getY(),blockPos2.getY());
        int y2=Math.max(blockPos1.getY(),blockPos2.getY());
        int z1=Math.min(blockPos1.getZ(),blockPos2.getZ());
        int z2=Math.max(blockPos1.getZ(),blockPos2.getZ());

        for (int i=0;i<=x2-x1;i++) {
            for (int j=0;j<=y2-y1;j++){
                for (int k=0;k<=z2-z1;k++) {
                    BlockPos blockPos = new BlockPos(i,j,k);
                    BlockPos real = new BlockPos(x1+i,y1+j,z1+k);
                    BlockState blockState = world.getBlockState(real);
                    BlockInfo blockInfo = new BlockInfo(blockPos,blockState);
                    blockInfoList.add(blockInfo);
                }
            }
        }

        System.out.println("保存完成，总方块数:" + blockInfoList.size());
        return blockInfoList;
    }

    public static BlockPos onPlace(BlockPos click,World world,List<BlockInfo> blockInfoList) {
        int i = 0;
        BlockPos start = null;
        for (BlockInfo blockInfo:blockInfoList) {
            BlockPos blockPos = new BlockPos(click.getX()+blockInfo.blockPos.getX(),click.getY()+blockInfo.blockPos.getY(),click.getZ()+blockInfo.blockPos.getZ());
            if (blockInfo.blockState.getBlock().is(Blocks.DIAMOND_BLOCK)) {
                start = blockInfo.blockPos;
            }
            if (world.setBlock(blockPos,blockInfo.blockState,3)) {
                i++;
            }
        }
        return start;
    }


}
